package com.agh.portal.web;

import java.util.List;

import org.joda.time.format.DateTimeFormat;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.agh.portal.domain.Ad;
import com.agh.portal.domain.AghGroup;
import com.agh.portal.domain.Answer;
import com.agh.portal.domain.Comment;
import com.agh.portal.domain.GroupEvent;
import com.agh.portal.domain.MainPagePost;
import com.agh.portal.domain.Post;
import com.agh.portal.domain.Question;
import com.agh.portal.domain.Student;
import com.agh.portal.domain.enumeration.AdvertisementCategory;
import com.agh.portal.domain.enumeration.PostCategory;
import com.agh.portal.domain.security.User;
import com.agh.portal.domain.util.CustomDateUtils;
import com.agh.portal.domain.util.SortTool;

@RequestMapping("/admin")
@Controller
public class AdminController {

	@Secured("ROLE_ADMIN")
	@RequestMapping(value="/student/list", produces = "text/html")
    public String studentList(
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, 
    		Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            List<Student> students = Student.findStudentEntries(firstResult, sizeNo);
        	for(Student s : students){
    			s.setLocked(User.findUsersByEmailAddress(s.getEmail()).getSingleResult().getLocked());
        	}
            uiModel.addAttribute("students", students);
            float nrOfPages = (float) Student.countStudents() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
        	List<Student> students = Student.findAllStudents();
        	for(Student s : students){
    			s.setLocked(User.findUsersByEmailAddress(s.getEmail()).getSingleResult().getLocked());
        	}
            uiModel.addAttribute("students", students);
        }
        uiModel.addAttribute("student_dateofbirth_date_format", DateTimeFormat.patternForStyle("M-", LocaleContextHolder.getLocale()));
        return "admin/student/list";
    }
	
	@RequestMapping(value="/ad/list", produces = "text/html")
    public String adList(
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size,
    		@RequestParam(value = "category", required = false) String category,
    		@RequestParam(value = "sortFieldName", required = false) String sortFieldName,
    		@RequestParam(value = "sortOrder" , required = false) String sortOrder,
    		Model uiModel) {
		if (page != null || size != null || category != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            AdvertisementCategory adCategory = AdvertisementCategory.parseEnum(category);
            sortFieldName = sortFieldName == null ? "creationDate" : sortFieldName;
            sortOrder = sortOrder == null ? "desc" : sortOrder;
          
            if(adCategory != null){
            	uiModel.addAttribute("ads", Ad.findAdEntriesByCategory(adCategory, firstResult, sizeNo, sortFieldName, sortOrder).getResultList());
            }else{
            	uiModel.addAttribute("ads", Ad.findAdEntries(firstResult, sizeNo, sortFieldName, sortOrder));
            }
            
            float nrOfPages = (float) Ad.countAds() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("ads", Ad.findAllAds());
        }
		uiModel.addAttribute("ad_creationdate_date_format", CustomDateUtils.patternForStyle("M-", LocaleContextHolder.getLocale()));
        return "admin/ad/list";
    }
	
    @RequestMapping(value="/question/list", produces = "text/html")
    public String questionList(
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, 
    		Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            uiModel.addAttribute("questions", Question.findQuestionEntries(firstResult, sizeNo));
            float nrOfPages = (float) Question.countQuestions() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("questions", Question.findAllQuestions());
        }
        uiModel.addAttribute("question_creationdate_date_format", DateTimeFormat.patternForStyle("M-", LocaleContextHolder.getLocale()));
        return "admin/question/list";
    }
    
    @RequestMapping(value="/mainpagepost/list", produces = "text/html")
    public String mainpagepostList(
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, 
    		@RequestParam(value = "category", required = false) String category,
    		@RequestParam(value = "sortFieldName", required = false) String sortFieldName,
    		@RequestParam(value = "sortOrder" , required = false) String sortOrder,
    		Model uiModel) {
    	if (page != null || size != null || category != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            PostCategory postCategory = PostCategory.parseEnum(category);
            sortFieldName = sortFieldName == null ? "creationDate" : sortFieldName;
            sortOrder = sortOrder == null ? "desc" : sortOrder;
           
            if(postCategory != null){
            	uiModel.addAttribute("mainpageposts", MainPagePost.findMainPagePostEntriesByCategory(postCategory, firstResult, sizeNo, sortFieldName, sortOrder).getResultList());            	
            }else{
            	uiModel.addAttribute("mainpageposts", MainPagePost.findMainPagePostEntries(firstResult, sizeNo, sortFieldName, sortOrder));
            }
            
            float nrOfPages = (float) MainPagePost.countMainPagePosts() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
        	uiModel.addAttribute("mainpageposts", MainPagePost.findAllMainPagePosts());
        }
    	uiModel.addAttribute("mainPagePost_creationdate_date_format", DateTimeFormat.patternForStyle("M-", LocaleContextHolder.getLocale()));
        return "admin/mainpagepost/list";
    }
    

    @RequestMapping(value="/aghgroup/list", produces = "text/html")
    public String aghgroupList(
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, 
    		@RequestParam(value = "sortFieldName", required = false) String sortFieldName,
    		@RequestParam(value = "sortOrder" , required = false) String sortOrder,
    		Model uiModel) {
    	
    	List<AghGroup> aghGroups = AghGroup.findAllAghGroups();
    	
   		if(sortFieldName != null){
			SortTool sortTool = new SortTool();
			String sortProperty = (sortOrder!=null) ? sortFieldName+":"+sortOrder : sortFieldName;
		    aghGroups = (List<AghGroup>) sortTool.sort(aghGroups, sortProperty);
		    uiModel.addAttribute("sortFieldName", sortFieldName);
		    uiModel.addAttribute("sortOrder", sortOrder);
		}
    	
    	if(page!=null || size !=null){
            int sizeNo = size == null ? 10 : size.intValue();
            final int fromIndex = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            int toIndex = fromIndex+sizeNo;
            if(fromIndex >= 0 && toIndex <= aghGroups.size() && fromIndex <= toIndex){
            	aghGroups = aghGroups.subList(fromIndex, toIndex);
            }
            uiModel.addAttribute("aghgroups", aghGroups);
            float nrOfPages = (float) AghGroup.countAghGroups() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
		}else {
            uiModel.addAttribute("aghgroups", aghGroups);
        }
        return "admin/aghgroup/list";
    }
    
    @RequestMapping(value="/answer/list", produces = "text/html")
    public String answerList(
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
              
            List<Answer> answers = Answer.findAnswerEntries(firstResult, sizeNo);

            uiModel.addAttribute("answers", answers);
            
            float nrOfPages = (float) Answer.countAnswers() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("answers", Answer.findAllAnswers());
        }
        return "admin/answer/list";
    }
    

    @RequestMapping(value="/comment/list", produces = "text/html")
    public String commentList(
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, 
    		Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            uiModel.addAttribute("comments", Comment.findCommentEntries(firstResult, sizeNo));
            float nrOfPages = (float) Comment.countComments() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("comments", Comment.findAllComments());
        }
        return "admin/comment/list";
    }
    

    @RequestMapping(value="/groupevent/list", produces = "text/html")
    public String groupeventList(
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, 
    		Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            uiModel.addAttribute("groupevents", GroupEvent.findGroupEventEntries(firstResult, sizeNo));
            float nrOfPages = (float) GroupEvent.countGroupEvents() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("groupevents", GroupEvent.findAllGroupEvents());
        }
        uiModel.addAttribute("groupEvent_eventdate_date_format", DateTimeFormat.patternForStyle("M-", LocaleContextHolder.getLocale()));
        return "admin/groupevent/list";
    }
    

    @RequestMapping(value="/post/list", produces = "text/html")
    public String postList(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            uiModel.addAttribute("posts", Post.findPostEntries(firstResult, sizeNo));
            float nrOfPages = (float) Post.countPosts() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("posts", Post.findAllPosts());
        }
        uiModel.addAttribute("post_postdate_date_format", DateTimeFormat.patternForStyle("M-", LocaleContextHolder.getLocale()));
        return "admin/post/list";
    }    
	
}
